package com.resou.reader.utils;

import android.content.res.Resources;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by ${Garrett} on 2018/9/3.
 * Contact me krouky@outlook.com
 */
public class ViewSizeUtils {
    /**
     * 获取状态栏高度
     */
    public static int getStatusBarHeight(Resources resources) {
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    /**
     * 获取RecyclerView滑动的y距离
     */
    public static int getRecyclerViewDy(RecyclerView recyclerView) {
        LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
        if (lm != null) {
            //获取RecyclerView当前顶部显示的第一个条目对应的索引
            int position = lm.findFirstVisibleItemPosition();
            //根据索引来获取对应的itemView
            View firstVisibleChildView = getRecyclerViewFirstView(recyclerView);
            //获取当前显示条目的高度
            int itemHeight = firstVisibleChildView.getHeight();
            //获取当前RecyclerView 偏移量
            return (position) * itemHeight - firstVisibleChildView.getTop();
        }
        return 0;
    }

    /**
     * 获取RecyclerView顶部ItemView
     */
    public static View getRecyclerViewFirstView(RecyclerView recyclerView) {
        LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
        //获取RecyclerView当前顶部显示的第一个条目对应的索引
        int position = lm.findFirstVisibleItemPosition();
        //根据索引来获取对应的itemView
        return lm.findViewByPosition(position);
    }
}
